package com.lottery.ruok.lottrey.chat.model.prize.message.view;

import android.content.Context;
import android.net.Uri;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.chat.model.prize.message.PrizeMessage;
import com.lottery.ruok.lottrey.ui.component.BaseRelativeLayout;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;
import glideHelp.GlideHelper;
import io.rong.imkit.userInfoCache.RongUserInfoManager;
import io.rong.imlib.model.UserInfo;

public class PrizeView extends BaseRelativeLayout {
    @BindView(R.id.tv_reward_from_username)
    TextView tvRewardFromUsername;
    @BindView(R.id.view_to_avatar)
    CircleImageView viewToAvatar;
    @BindView(R.id.tv_to_username)
    TextView tvToUsername;
    @BindView(R.id.view_prize)
    LinearLayout viewPrize;
    private PrizeMessage data;

    public PrizeView(Context context) {
        super(context);
    }

    public PrizeView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PrizeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    public void setData(PrizeMessage data) {
        this.data = data;
        updateView();
    }

    private void updateView() {
        if (data != null) {

            UserInfo receiveUser = RongUserInfoManager.getInstance().getUserInfo(data.getReceiveUserId());
            UserInfo sendUser = RongUserInfoManager.getInstance().getUserInfo(data.getSendUserId());
            if (receiveUser!=null){
                tvToUsername.setText(receiveUser.getName());
                GlideHelper.loadImage(mContext, receiveUser.getPortraitUri().toString(), viewToAvatar);
            }else {
                tvToUsername.setText(data.getReceiveUserNickname());
                GlideHelper.loadImage(mContext, data.getReceiveUserAvatar(), viewToAvatar);
            }
            if (sendUser!=null){
                tvRewardFromUsername.setText(sendUser.getName());
            }else {
                tvRewardFromUsername.setText(data.getSendUserNickname());

            }

        }
    }

    public void setRedPacketBackgroundResource(int resId) {
        viewPrize.setBackground(mContext.getResources().getDrawable(resId));
    }

    @Override
    protected void init(Context context, AttributeSet attrs, int defStyleAttr) {

    }

    @Override
    protected void initView(Context context) {
        setContentView(R.layout.view_prize);
    }

    @Override
    protected void load(Context context) {

    }
}
